//
//  WLMHomeCellModel.swift
//  WeiBo
//
//  Created by Eric on 2017-01-09.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit

class WLMHomeCellModel: NSObject {
    var headURl: URL?
    
    //等级ICON
    var mbrankIcon: UIImage?
    
    var verifiedImage: UIImage?
    
    var sourceText: String?
    
    var timeText: String? {
        let date = DateFormatter()
        date.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        date.locale = Locale(identifier: "en")
        guard let sinaDate = date.date(from: status?.created_at ?? "") else {
            return "你不知道你哪里来的"
        }
        let calendar = Calendar.current
        let currentDate = Date()
        let result = calendar.dateComponents([.year, .day], from: sinaDate, to: currentDate)
        
        if result.year == 0 {
            if calendar.isDateInToday(sinaDate)
            {
                let delta = currentDate.timeIntervalSince(sinaDate)
                if delta < 60
                {
                    return "刚刚"
                }
                else if delta < 3600
                {
                    return "\(Int(delta / 60))分钟前"
                }
                else
                {
                    return "\(Int(delta / 3600))小时前"
                }
            }
            else if calendar.isDateInYesterday(sinaDate)
            {
                date.dateFormat = "昨天 HH:mm"
                return date.string(from: sinaDate)
            }
            else
            {
                date.dateFormat = "MM-dd HH:mm"
                return date.string(from: sinaDate)
            }
        }
        else
        {
            date.dateFormat = "yyyy-MM-dd HH:mm"
            return date.string(from: sinaDate)
        }
    }
    
    var imageURLS: [WLMPictureInfo]? {
        if status?.retweeted_status == nil {
            
            return status?.pic_urls
        }
        
        return status?.retweeted_status?.pic_urls
        
    }
    
    var status: WLMStatus? {
        didSet{
            headURl = URL(string: status?.user?.avatar_large ?? "")
            self.setVerifiedImage()
            self.setMbrankIcon()
            self.sourceText = dealWeiboSource(source: status?.source ?? "")
        }
    }
    
    private func dealWeiboSource(source: String) -> String {
        let startFlag = "\">"
        let endFlag = "</a>"
        guard let startRange = source.range(of: startFlag),
            let endRange = source.range(of: endFlag) else {
                return "来自火星"
        }
        
        //upperBound 最大边界 lowerBound: 最小边界
        let range = startRange.upperBound..<endRange.lowerBound
        let subStr = source.substring(with: range)
        return subStr
    }

    
    private func setMbrankIcon() {
        let mbrank = status?.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 7 {
            mbrankIcon = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        else{
            mbrankIcon = #imageLiteral(resourceName: "common_icon_membership_expired")
        }
    }
    
    private func setVerifiedImage() {
        let verifiedType = status?.user?.verified_type ?? -1
        switch verifiedType {
        case 0:
            verifiedImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            verifiedImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            verifiedImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            print("没有认证")
        }
    }
    
    
}
